//
//  ComposePhotosView.m
//  CMJWeibo
//
//  Created by BH on 16/1/7.
//
//

#import "ComposePhotosView.h"
#import "UIView+MJ.h"

@implementation ComposePhotosView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

-(void)addImage:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = image;
    [self addSubview:imageView];
}

-(NSArray *)images
{
    NSMutableArray *imagesArr = [NSMutableArray array];
    for (int i=0; i<self.subviews.count; i++) {
        UIImageView *imageV = self.subviews[i];
        [imagesArr addObject:imageV.image];
    }
    return imagesArr;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置所有图片的frame
    CGFloat margin = 10;// 图片之间的间距
    NSInteger maxCols = 4;//一行显示4列
    CGFloat imageW = (self.width-(maxCols+1)*margin)/maxCols;
    CGFloat imageH = imageW;
    
    for (int i=0; i<self.subviews.count; i++) {
        UIImageView *imageView = self.subviews[i];
        imageView.width = imageW;
        imageView.height = imageH;
        NSInteger col = i%maxCols;
        imageView.x = margin+col*(imageW+margin);
        NSInteger row = i/maxCols;
        imageView.y = row*(imageH+margin);
    }
}

@end
